Skip to main content

Full text of "Moving Graphics"

See other formats


r 


v/Kffi youK own pnoomini 



1 

1 







































































- ^ ^ ^ 

yifRITE yOUR. OWN PROGRAM 

-r«5 /vfw book 

pko&kbmm'NO rouR w into 

n.oinm% 

qt^p PNftiyzBS f,pp p/HY- 

flSRtf/0 COMPUTBB. 

aun W£/e£'S PP!^ CONTPP^ 

ffh/Pl^HBR j-^rjriNQ PNP OfZlGl^NL 

ONE OR CNN BE PP^f^EP 

COMPU'rtH <?/9A/£5. fJNP 

RNP B^raNPEP PO 8I00EB » 

sarrBR. rmos. 

ocriPiC. -TO COMMOPORB64‘ 

the boons m ^ 

/^yp flFPLBUe.. 


TITLES IN THE SERIES 
BEGjINNIN(3 basic - SPACE ..JOURNEY 
GRAPHICS ” HANGMAN 
MOVING GRAPHICS -- ALIEN INVADERS 
CREATING A DATABASE - ADVENTURE GAME 


ISBM 0--531 “03491 “7 


A GLOUCESTER PRESS LIBRARY EDITION 
































First published in 
Great Britain in 1985 by 
Franklin Watts 
12a Golden Square 
London W1 

First published in the 
United States in 1985 by 
Gloucester Press 

Gopyright © Aladdin Books Ltd 1985 

Printed in Belgium 

ISBN 0 531 03491 7 

Library of Gongress 
Gatalog Gard Number: 84-73147 
























mn ym 



Marcus Milton 



GLOUCESTER PRESS 

NEW YORK ■ TORONTO • 1985 












































/ 


F' 



Foreword 


If you have a computer, then you are almost certain to have 
played a Space Invaders game of one kind or another. In this 
book, you'll find a program that allows you to create an 
"invaders" game of your own, in full color. The program is 
given for the Apple He and Commodore 64 computers. 

An invaders game relies upon moving graphics to be 
effective. The principle is similar to those used in cartoons - 
old characters are erased from the screen before being 
printed in a new position, giving the impression of 
movement. The program has been broken down to its 
logical stages, and there is a running text which explains 
how each section works. You will find, for example, just how 
it is that the computer “knows" when an invader has been 
shot by one of your missiles. Writing a program in this way 
not only makes it easier for you and other people to 
understand, it also makes it run more quickly and reduces 
the possibility of errors. At various stages you can test the 
sections of program, that you’ve keyed in - even the smallest 
error can mean that the program doesn't work. If something 
does go wrong check back through the listing very carefully. 
Looking for “bugs" in a program is like playing a detective 
game - the clues are there for you to spot them. 












































C#=:.. " 

^O PRii^jj'. 

>0 X=0:y-J 

print T 

0 PRiisiT r, 

print 77 

' '=’PINT Tfi 
' '=‘P1NT ta 
print TAi 

Tftc 


!:^PIGHT nrur- 

-I^HRt2sscHKt, 

"^'*^53CHR:$:2S- 


^pscrnt 


'"SCHRt, 


Contents 


Introducing animation 



8 

User defined characters 



10 

The flowchart 



12 


1 THE CONTROL PROGRAM AND INITIALIZATION 

13 

APPLE He 

14 

COMMODORE 64 

18 

2 MOVING AND FIRING 

23 

'apple He 

24 

COMMODORE 64 

28 

3 WINNING AND LOSING 

33 

APPLE He 

34 

COMMODORE 64 

36 

The complete listing 

40 

Glossary 

42 

Index 

44 


















































Introducing animation 

One of the simplest forms of computer animation is achieved 
by printing a figure on the screen, erasing it and then 
quickly printing the same figure again in a slightly different 
position. Repeating this action a number of times gives the 
illusion of the figure moving rapidly across the screen. This 
short program demonstrates the effect. 


o 


;!.() HOME 




20 FOR ;i; 


T‘0 

3S 

30 VTAB 5s 

HTAB 

I:; PRINT " X “ 

40 FDR D 

•1 

TO 

lOOs NEXT D 

ilEXT I 









u 


© 

© 


The letter X is moved across the screen using a FOR... 
NEXT loop, with I as its variable - the changing value of I 
gives different screen coordinates for printing “X”. Notice the 
space either side of the X. When the letter is moved one 
space along to the right, the spaces either side move with it. 
The blank space on the left will overprint the old X and 
effectively erase it from the screen. 

In line 40 a short delay loop is introduced, making the 
computer count to 100 (Apple lie) or 200 (Commodore) 
before it prints the next “X”. Without the delay loop, the 
computer would perform the whole operation so quickly it 
would be impossible to follow. Delete line 40 and see what 
happens. 




;l C) F’R .1 N T " ki.," ; PR I N T' ” 

20 FOR 1=1 TO 38 
30 PRINTSPC < I) "□ K"- 
40 FOR D:=l T O 200;; tie;XT D 
50 NEXT 1 




8 































































I OF 'X' 


Commodore Sprites 

In addition to the move-and-blank technique described 
opposite, the Commodore has its own special moving 
graphics called sprites. 

Sprites do not have to be blanked out and reprinted many 
times to make them appear to move. Instead, you will have to 
use the special registers in the table below. Later in the 
book you will see how, by POKEing numbers into these 
registers, sprites for missiles and explosions can be created 
and controlled. 

Sprites are a complex, but very flexible feature of the 
Commodore, and there are many more special sprite 
registers. Unfortunately, in this book there isn't room to much 
more than show you a small percentage of the possibilities 
open to you, but with practise, you will be able to create 
stunning animated graphics. 


Memory 

Location 

Name 

832 

start of sprite data area 

53248 

X location register 

53249 

Y location register 

53259 

sprite enabling register 

53264 

X register in VIC (video) chip 

53269 

Y register m VIC (video) chip 

53287 

color register - sprite one 

53289 

color register - sprite two 


5 

9 

i 


i 


i- 















































































j 


|j 



i. 


i 


4 


I 







User defined characters 


Games can be made to look far more exciting and 
professional by defining your own user defined graphics, 
which can be animated using the techniques you will see 
later in the book. In the Commodore program you will be 
using sprites. 

APPLE He 

Defining graphics on the Apple involves using shape tables. 
These can be very difficult to use until you become familiar 
with them. The screen is made up of many dots (or pixels), 
and you can instruct your Apple to draw shapes by joining 
these dots together. There are eight instructions, numbered 
in binary 000 to 111. Instructions 000 to 011 are called move 
only operations, which tell the computer to move a pointer 
either up, down, left or right one pixel on the screen. 


TmLES 

move only 

(oooooooo) 


plot fINP MOVE 
4 

(oooootoo) 


3 ^ ^^^^^(ooooofoO 

(OOOOOOf/J ^ ■ JoOOOOOOl)(OOOOOIllj 

2 (ooooouo) 

(oooooo/o) 


TO MEKB N SHPF^ 

'(OOIIOIOI) 


(oaioioi) 


(oonono) 


With a move only instruction you can move an imaginary 
pointer around the screen without drawing anything. 
Instructions 100 to 111 are plot and move operations - these 
draw as well as move. Starting from a defined point on the 
screen, you use the move only instructions to move the 
pointer to where you wish to commence drawing, before 
using the plot and move instructions to draw it. 


The computer doesn’t understand these instructions as they 
stand, as 3-bit (binary digit) numbers. They first need to be 
collected together into 8-bit numbers, and then converted to 
decimal numbers (the kind of numbers humans count in). 
Each 8-bit number contains two shape table operations, 
usually preceded by two zeroes. You can convert from 
binary to decimal by imagining each bit as a column 
representing a decimal number - starting from the right, the 
columns represent 1, 2, 4, 8, 16, 64, and 128 - and adding the 
columns together. 


10 














































































lOIOl) 


COMMODORE 

Computer graphics screens consist of many small dots (or 
pixels). Design your sprite on graph paper using a 24- by 
21-pixel grid. You can draw any shape you like, shading 
squares that you want the computer to color. It understands 
that each dot can have the value of 0 or 1. If the dot is set to 1, 
then the computer interprets this as a filled dot. If the dot is 
set to 0, the computer takes the dot as blank. These Is and Os 
are known as binary digits (or bits). There are eight bits to a 
byte, and the computer stores each byte in a memory 
location. Sprites are defined by POKEing 63 bytes into a 
special area of memory. POKE tells the computer to put a 
number into a specific memory location. 

Sprites can be any size up to 24 by 21 pixels. The diagram 
below shows how the missile sprite used later on in the book 
5 drawn and how the DATA is calculated. 



3 00 

60 00 
95 00 
IS 00 
2JS0 0 
60 00 
111 0 0 
in 00^ 
0 00 
0 00 
000 
0 00 
0 00 
ooo 



Setting up a sprite involves a number of steps, using some 
special registers in the Commodore. First, DATA must be 
POKEd into an area of memory starting at location 832. The 
DATA has to be converted from the eight bit binary 
numbers into decimal numbers (the numbers that humans 
understand, and programs use) before it can be put into a 
program. You can do this by adding up the numbers at the 
top of the columns in the diagram above. Up to eight sprites 
can be set up at the same time, each needing their own 
sprite pointer so the computer knows which sprite you want 
to use. Next, the sprite needs to be turned on using the sprite 
enehling register (location 53248-1-21). The computer needs 
to know what color to make the sprite, so a color code must 
be POKEd into the color register (location 53248-1-39 for 
sprite 1, location 53248-1-40 for sprite 2, and so on). Finally, 
the sprite needs to be positioned on screen, using X and Y 
location registers. You will see how the sprites for the missile 
and explosion used in the game are set up in this program 
later in the book. 



11 






























































































































































The flowchart 

To plan the Invaders game, it's a good idea to draw up a 
flowchart. This is a convenient way of setting out the series of 
logical steps that define your program. It consists of different 
shaped boxes, representing decisions, actions and so on, 
connected by "flowlines". The flowchart is used to create a 
CONTROL PROGRAM. This is the core of the program, 
which calls up other sections of the program whenever they 
are required. 




r 

'P 

erL 

flV 

■ J 

AW 







































































































































































































































































































































































































































































































































































































































































THE CONTROL PROGRAM 
AND INITIAUZATION 

The control program shows how the 
program is structured, using a series of logical 
steps called subroutines on the Apple 
and Commodore machines. These are grouped together 
on the flowchart to show how the control program has 
been constructed. The first section 
of the program then displays instructions 
for the game on the screen. 


V O U 1=1 R E 1=1 LONE S P C E P I E O I 

PROTECT i: HG T i ^ RF * HHDT R 

O EE 14 MOMEN IS VOO *4J EE BE 
OTTOCK BV OE IEHS F ROM I HE PEoNE I 

H f l n 11 M T S S r O N 1 S T o P R E U E H T O N V 

6 O R G O N I iPli H ■ ■ S H i; P E R O M I... O H l> .1. N G . 

' y ' Hfl«P E G S1' 1=1 R E I H T E R . l-r E E T 

K "" MO U E S * S E H-H R E' 'X- Ei 1*1 I" E. R R 1 O H I 

P R E S S S1=' O C E T O F I R E H I! S S :i 1... E 

PRESS <=»NV k:ev to EHGOGE ENEHV 







































































































By following the flowchart on page 12 and breaking the 
program into subroutines placed in a logical order, you can 
arrive at the main program for the game, as shown below. 
Once you have a main program like this, all you have to do is 
write each subroutine in turn. 




REM 

INVADERS 


0 

10 

GOSUB 

8000 

REM 

INITIALIZE 

20 

GOSUB 

70C)0 s 

REM 

SCREEN 


30 

GOSUB 

. 1 . 000 : 

REM 

ALIEN 

0 

40 

GOSUB 

2000 s 

REM 

PL.AYER 


50 

IF" WIN == 1 

OR L..OSr == .1 THEN 

0 

60 

GOTO 

30 




70 

IF WIN == 1 

THEN 

GOSUB 5000: 

o 

80 

GOSUB 

6000: 

REM 

LOSE 

90 

END 








The first line of a program usually gives its title, by way of a 
REMark - see line 5. REMarks help anyone see what is 
happening in a program. At the beginning of a game you 
should give the player instructions, and set all the variables 
to give the player instructions, and set all the variables to 
their initial values. Line 10 does this. The colon (:) allows 
separate statements to be placed on the same line. The 
subroutine which plots the stars and prints the score is called 
by line 20. 

The next four lines, 30 to 60, form a repeated loop which is 
the heart of the program. A randomly selected alien is 
moved one position down the screen in line 30. Line 40 
allows the player to move and fire a rocket at the aliens. Next 
comes an IF... THEN test in Line 50. IF either of these 
conditions are true, THEN the computer jumps out of the 
loop to line 70. Otherwise, line 60 sends the computer back 
to line 30, ready to repeat the move/fire process. WIN and 
LOST are simply "flags” which can be set true or false (1 or 0) 
to tell the computer how the game has ended. Line 70 tests if 
the WIN flag is set. IF it is, THEN the WIN subroutine is 
called, using the instruction GOSUB 5000. The computer 
GOes TO line 20 for a fresh screen of aliens, and a new 
game. If WIN isn’t set, the program calls the LOSE 
subroutine, starting at line 6000. All the subroutines follow 
later in the book. 



The END statement may be placed anywhere that is 
convenient. However, it is good practice to place the END at 
the end of the control program. This gives a neat finish and 
makes the program more readable. 


14 


























































BEFOf^E you START 
IT tS A GOOD lOEA 
TO Pt-AH HOW yOUR 
SCREEN WILL LOOK 
ON GRAPH PAPER- 



The first subroutine called is INITIALIZE (see below). It has 
been put at line 8000 because, every time the computer 
looks for a subroutine it starts at line 1, looking at each line in 
turn. Subroutines which you use frequently should be put at 
the start of the program. In line 8010 we clear the text 
screen, and bring the cursor to the home position (to the top 
left hand corner of the screen) using call TEXT. VTAB means 
vertical tab, or move down a certain number of text lines on 
screen. HTAB is exactly the same but for horizontal 
movement. In line 8020, the computer is instructed to start 
PRINTing down two lines, and across seven characters. 




S3 >000 REM 
801,0 TEXT 
8*01::! >L^ 'v I (-1.8 
p :i: t...0T" 

8030 HTAB 
S:i040 HTAB 
8050 HTAB 
S306'0 i'lrAB 
8070 HTAB 
8080 HTAB 
8090 VTAB 
LEFT " 

81.'0>0 VTAB 


INITIALIZE 
; HOME 

2s HTAB 7s PRINT "YOU ARE A LONE SPACE 

:5s PRINT "PROTECTING THE PLANET EARTH. IN" 
5s PRINT "A FEW MOMENTS YOU WILL BE UNDER" 
5s PRINT "ATTACK BY ALIENS FROM THE PLANET" 
IBs PRINT "VARGON." 

5s PRINT "YOUR MISSION IS TO PREVENT ANY" 

5s PRINT "VARGONIAN SHIP FROM LANDING." 

1.0 5 HTAB 7s PRINT " ' Z ' MOVES STARFIGHTER 

1.2 s H T A B 7 s P RIN T " ' X ' M 0 V E S S T A R FIG H T E R 


RIGHT" 

81.1.0 VTAB 14 5 HTAB 7s PRINT "PRESS SPACE TO FIRE 
MISSILE" 

81.20 VTAB ISs HTAB Ss PRINT "PRESS ANY KEY TO ENGhGE 
ENEMY "5 

























































































































































































































































































































0:HrTSTAR 
= INVADERS 


0: XP 


> GOSUB 9000 

') LOST = OsWIM = Or. SC = Os AH IT 
H EI ("j H r == 0 s IM V A D E R '3 “ 10 s A LIE N 

) ALIENS == INVADERS _ .. 

DI IT X ( INV ADERE3) s DI IT Y ( INVADERS) s DI IT GD"/. (-s-o , 1. X) 

FOR A 1 TO INVADERS 
D X(A) - A * 3sY(A) = HEIGHT 

NEXT A 
GET RT 
RETURN 





As the player is reading the instructions, the computer can 
be getting on with initializing the variables used in the game. 
In line 8140 a number of flags are set. LOST and WIN are set 
to zero since the game has yet to begin. SC represents the 
number of aliens that have been shot down. AHIT (alien hit) 
and HITSTAR are two flags which check if a missile has 
collided with an alien or a star. XP is the position of the 
player’s spaceship along the screen’s X-axis. HEIGHT is the 
Y position of the space invaders when they start towards the 
top of the screen. In Line 8150 the number of ALIENS is set 
by INVADERS. This extra variable is used to make it very 
easy to change the number of space invaders at the start of 
the game. Once INVADERS has been altered, the initial 
value of ALIENS will automatically alter throughout the 
program. Each space invader is given its own label by 
DIMensioning two one-dimensional arrays in line 8160. 
DIMX(INVADERS) reserves memory space for the column 
position of each space invader, and DIMY(INVADERS) does 
the same for the row position. For example, X(2) refers to 
the second space invader working from the left of the 
screen. GD% is an integer (whole number) array which 
holds the location of all the characters. Line 8170 starts a loop 
which sets the initial positions of the INVADERS. Line 8180 
enters the X and Y position of each invader. The X position is 
determined by multiplying the alien’s number by three, and 
the Y position by HEIGHT. 





















































s>050 


- ^DATA 245,59,63,159,46,1 OS , 73,53^ 63 , UO ,.6^1 +.J , ^^8 

„ 46,5,00 , ]. 3,77,13 , ;l. 82,58,223,36,220, 1 4 / , ^8,61 , , K ^ , 1 ^ 

0 I 3,43, :l. 09,57 „ 4,193,36,4,19::';, 193,32,36,223,256 , ^ / , , .jW 

Ul 50 ; :l. 73,191,82,118,182,41,69,41,5,193,193,1 , OO 
ih 9060 RETURN _ 


Five shapes are defined for the game - the missile, the 
player's spaceship, the alien, the star and the explosion. The 
DATA in lines 9040 and 9050 is POKEd into memory by the 
FOR... NEXT loop between lines 9000 and 9030. In the 
DATA the first two numbers indicate how many shapes are 
being defined (in this case five). The following ten numbers 
form five pairs, telling the computer where the data for each 
shape begins. 

The SCREEN subroutine DRAWs a random background of 
stars before the game begins or when the player is skilful 
enough to shoot all the aliens. Line 7010 clears the screen 
and sets the screen to Page 1 of high resolution graphics 
(HGR). In line 7020, POKE - 16301,0 allows you to use the 
bottom four lines of the screen for text. POKEing locations 
232 and 233 tells the computer where to find the shape table 
information for the stars, and SCALE sets their size. 


20 RANPOM CHARACTeR 
blocks ARe CHOSBN 
for THB stars, I 
1 WHICH ARB POSITIONBP 
AS SHOWN- IF THB 
\ ALIBN MOVES INTO 
THIS BLOCK 'T WCT 
1 NOT PBLBTB THB STAR- 


7000 
7010 
7020 

s(::al.e: 

7 0^30 
7040 
9) 

7C)50 
'7060 
7070 
7080 
■7090 
7 :i. 00 


REIN S3t:;REEN 
l-IGR ; NOME 

P 01--; E - :i. 630 ]. , 0 !i P 0 K: EI 232,252: P ORE 2-33,29; 

== ]. 

FOR 3 = 1 TO 20 

DF = TNT ( RND (t) * 35):GH = TNT t RND (1) * 
GD%(DF,GH) = -J. 

HCOI....OR= 7s DRAW 4 AT 8 DF,8 GH 

next S 

VTAEvi 22 s HTAB 10s PR .TNT "AL. IE.NS DESTROYED- ; SC 
HCOLOR- 2s DRAW 2 AT 8 * XP,8 * 19 
RETt...lRN 


There is a FOR... NEXT loop between lines 7030 and 7070 
which plots 20 stars on the screen. Line 7040 uses your 
machine's random number generator to decide where to put 
the stars. Each time the computer goes through the FOR... 
NEXT loop an X and a Y coordinate are chosen. Line 7050 
stores the value one in the element in GD% which 
corresponds to the star position. Line 7090 DRAWs the 
spacecraft at the bottom of the screen, ready to start firing. 












































By folloAwing the flowchart on page 12 and breaking the 
program into subroutines placed in a logical order, you can 
arrive at the main program for the game, as shown below. 
Once you have a main program like this, all you have to do is 
write each subroutine in turn. 



5 REM IN' 

9ADERS 



10 

GOSUB 

8000 s 

REM 

INITIALIZE 

20 

GOSUB 

7000 s 

REM 

SCREEN 

30 

GOSUB 

1000 s 

REM 

ALIEN 

40 

GOSUB 

2000 s 

REM 

PLAYER 

50 

IF WIW=1 OR 

LOST 

=--.t THEN GOTO 70 

60 

GOTO 

30 



7"0 

IF WIh4=l THEN GOSUB 5000: GOTO 

80 

GOSUB 

6000 s 

REM 

LOSE 

90 

END 





20 




The first line of a program usually gives its title, by way of a 
REMark - see line 5. REMarks help anyone see what is 
happening in a program. 

At the beginning of a game you should give the player 
instructions, and set all the variables to their initial values. 
Line 10 does this. The colon (:) allows separate statements to 
be placed on the same line. The subroutine which plots the 
stars and prints the score is called by line 20. The next four 
lines, 30 to 60, form a repeated loop which is the heart of the 
program. 

A randomly selected alien is moved one position down the 
screen in line 30. Line 40 calls a subroutine allowing the 
player to move and fire a rocket at the aliens. Next comes an 
IF... THEN test in Line 50. IF either of these conditions are 
true, THEN the computer jumps out of the loop to line 70. 
Otherwise, line 60 sends the computer back to line 30, ready 
to repeat the move/fire process. WIN and LOST are simply 
"flags" which can be set true or false (1 or 0) to tell the 
computer how the game has ended. Line 70 tests if the WIN 
flag is set. IF it is, THEN the WIN subroutine is called, using 
the instruction GOSUB 5000. The computer immediately 
GOes TO line 20 for a fresh screen of aliens, and a new 
game. If WIN isn’t set, the program calls the LOSE 
subroutine, starting at line 6000. 

The END statement may be placed anywhere that is 
convenient. However, it is good practice to place the END at 
the end of the control program. This gives a neat finish and 
makes the program more readable. 















































































BBFOP-B yo<J START 
IT IS A GOOD IDEA 
TO PLAN HOW YOUR 
SCREEN WILL l-OOK 
ON GRAPH PAPER ■ 



The first subroutine is INITIALIZE. Line 8010 sets the screen 
colors by POKEmg locations 53280 (the border) and 53281 
(the background) with zero (black). POKE 650,255 sets the 
keyboard to auto-repeat so the spacecraft moves smoothly, 
and the heart-shaped control character clears the screen. Lines 
8020 to 8120 contain SPC (SPaCe) commands, to position the 
display. The control characters used determine the text color. 

___w 

8000 REH INITIALIZE _ . 

8010 POKE 53280,0: POKE 53281,0: POKE 

8020 PRINTSFE(6) "IwVOy ARE A I....OHE 
8030 PR INTSPC (4) "PR O T E C T IMG 1 1-1 E P E A H E T 
E R Ti"i I 

a 040 P RIN T S P C ( 4 ) " A ¥ E IM M O M E M T S V O U 141 L. L 
BE OMDER" 

8050 PR I N T SPC (4) " A T T A C K Et V A1.... I E M S F R O M 
THE PL.AMET" 

8080 PR I NTSPC (4) " U A R G O M" 

8070 PRINTSPC(4) " VOOR MISSIOM IS TO 

fh«e:uemt a MV" 

8080 PR I NTSF'C ( 4 ) " U A R Ei O M I A M S H I P FROM 
L.AMl> I MG . " 

8090 PR I NTSPC (6) " LlL-H Z " MO E S S T A R F 1 G H I E R 
8100 Ffa NTSPC (6) " ^ X MO U E S S T « I « »E ^ ” J " f 

8110 PRINTSPC(7) "IMPRESS SPAE'E" -JiSaGE 

8120 PR I NTSPC (5) " liLElSIP R E S S A M V REV TO ENGAGE 

EMEMV" 






































































































































































































































































































































































































































-O : 



L0ST--0; WIM=-Os SC--0: AH I T 
H EI e H T --i" IN ‘v A D E S " 1C) 

AL. I ENS = I NVADEFvS 

DIM X (INVADEr:;S) : DIM Y (INVADERS) s 
POP; p,= j. TO IMVADEF-^£) 

X A) = A *■ 4 2 s Y (A) “ F-1EI (3 F-l T 
GRID';':,(X (A) ,Y(A) )~2 
NEXT A 
GOSUB 8500 
RT-" " 

GET RT; IF RT="’' THEN 8220 
RETURN 


As the player is reading the instructions, the computer can 
be getting on with initializing variables. In line 8130 a 
number of flags are set. LOST and WIN are set to zero since 
the game has yet to begin. SC represents the number of 
aliens that have been shot down. AHIT is a flag which is used 
to check if a missile has collided with an alien. XP is the 
position of the player's spaceship along the screen's 
HEIGHT is the Y position of the space invaders. In line 8 14(J 
the number of ALIENS is set by INVADERS. If you alter the 
value of INVADERS, the initial value of ALIENS will 
automatically alter throughout the program. Each invader is 
given its own label by DIMensioning two arrays in line 8150. 
DIMX (INVADERS) reserves memory space for the column 
position of each space invader, and DIMY (INVADERS) does 
the same for the row position. Line 8160 starts a FOR... 
NEXT loop which sets the initial positions of the INVADERS. 
Line 8170 enters the X and Y position of each invader. 


HITSTAR=Os XF-20: 


DIM GRID"/: (40,21). 


,N^C6) 


E?.500 
8510 


8 !: 


h:) 


8530 

8540 

8550 

8560 

8570 

8580 



REM SPRITE DEFINITION 

F(1F; N~-0 TO 62: F;EAD Q: F'UF^E. 8.;'2“t'N, Q: NEXT. 

REM MISSILE 

PT)R N=0 to 62; F'(EAD Q; PUF=:.E 8V6+N„0; NEXT; 

REM EXPLOSION 
0=53248 
POKE .2040,13 
POKE 2041,14 
POKE V4-39,2 

POKE 04-40,7 . . ^ . .. 

DATA 8,0,0,28,0,0,28,0,0,62,0,O,93,O,O,28,0,O, 

78,0,0,62,0,0,127,0,0,12/,U,O _ _ _ 

DATA ' 0,0,0,0,0,0,0,0 , O , 0,0,0,0,0,0,0,0,0,0,0,0, 

O „ 0,0,0 , O , 0,0, O , 0,0,0,0 , w cr o 1 Q 

data 0,0,0,136,0,17,2,0,64,1 ,0,128,1 «:>, 16o , 8,1 , 

0,129,33,0,132,18,165,72 



20 
























































(•36 J. 0 DATA 12 „ 90 48 , J. 8, .16(5,72,0,92,0,0,92,0,72,165,18, 
48,90,12 

8620 DATA 72,165,18,132,0,33,129,0,129,8,165,16,128, 

0,1,64,0,2,17,0,136 
8630 f:;e;;tijrn 

9000 f-4-i: I NT ” la": !(>l< I I id Y: PF(lNT"ia" 5 : NEIXTs REITURN 








On pages 9 and 11 you saw how Commodore sprites are 
created and used in theory. This routine sets up the missile 
and explosion sprites needed for the game using the 
Commodore’s special registers. Line 8510 POKEs the 63 
bytes into memory, starting at location 832. Line 8530 makes 
V=53248 - this is the lowest numbered memory location and 
every higher numbered location only needs be numbered 
as V plus a small number. The sprite pointers are set up in 
lines 8540 and 8550. Next, the sprite needs to be turned on 
using the sprite enabling register (location V+21). 

The computer needs to know what color to make the sprite, 
so line 8560 POKEs a color code into a color register 
(location V+39 for sprite one, location V+40 for sprite 2, and 
so on). In this case the code is 2, which is the equivalent to 
red sprite graphics. 



■0 

0 

0 



7000 REM SCREEN | 

7010 POKE V-+-21,Os PR I NT "U" m. | 

7020 FOR S=1 TO 40 ^ I 

7030 X:= 1 NT ( RND (1 i -i^ ^O) : Y= I NT (RND (2) *21) ; PR I NT " 653” ; | 

GOSUB 9000;PRINTTAB(X)"Lg, ” 0 | 

'7040 OR I Da < X , Y > =--: 1 
7050 NEXT 3 

7060 PR :i: NT ” 653" : PR 1 N T SPC (10) " A 8 ... I E 8^ S S> E S T' IS O V E l> ” ; SC 
7070 RETI..JRN 



Line 20 of the control program instructs the computer to 
GOSUB 7000. This subroutine draws a random background of 
stars. Line 7010 clears the screen, ready for the background. 
There is a FOR... NEXT loop between lines 7020 and 7050 
which plots 40 stars on the screen. Line 7030 uses your 
machine's random number generator to decide where to put 
the stars. Each separate part of an array is called an element, 
and a two-dimensional array such as GRID% can be used to 
represent a map by making each element correspond to a 
grid square on the map. Line 7040 stores the value one in the 
element in GRID% which corresponds to the star position 
that has just been chosen. 




21 












































At this point it is a good idea to test the program. Follow the 
instructions in the box below and you should see the title 
screen. Pressing any key will bring up the second screen. If 
the program doesn't work, or something peculiar happens, 
check through your work very carefully. Even tiny errors 
can cause the whole program to crash. 


oao! err <■ TVPS A DUMMY LINE 2S B NO A ND HIT RETURN (SO 
nHHLt.Ue 2. TYPE RUN AND HIT RETURN CSO 

YOU SHOULD NOW SEE THE TITLE SCREEN SHOWN LEFT. PRESSING ANY KEY WILL 
ALLOW THE PROGRAM TO CONTINUE, AND THE STAR BACKGROUND WILL BUILD UP, 
SHOWN RIGHT. DELETE THE DUMMY LINE 25 BEFORE PROCEEDING TO THE NEXT 


SECTION. 



YOU ARE A LONE SPACE PILOT 
PROTECTING THE PLANET EARTH. IN 
A FEW MOMENTS YOU WILL BE UNDER 
ATTACK BY ALIENS FROM THE PLANET 
VARGON. 

YOUR MISSION IS TO PREVENT ANY 
VARGONIAN SHIP FROM LANDING. 

'Z' MOVES STARFIGHTER LEFT 
X' MOVES STARFIGHTER RIGHT 
PRESS SPACE TO FIRE MISSILE 


PRESS ANY KEY TO ENGAGE ENEMY 



A PC/MMY L/A/E a S END THBN H/T 
2 Ri/Ai THEN N/T /Ren^ 

you SHOULD NOW SEE THE TITLE SCf^BEN SHOWN LEFT. PRESSING ANY KEY WILL 
ALLOW THE PROGRAM TO CONTINUE, AND THE STAR BACKGROUND WILL BUILD UP, 
SHOWN RIGHT. DELETE THE DUMMY LINE :25 BEFORE PROCEEDING TO THE NEXT 
SECTION. 

















































































In this section of program the aliens move at ranaom aown 
and across the screen. It also contains the instructions which 
allow the player to move his or her spaceship and to fire 
missiles to shoot the aliens down. A running score of the 
number of aliens hit is given at the top of the screen. 
Good Luck! 








































The ALIEN subroutine given below has a low number 
because it is the most frequently used of the subroutines. 

The subroutine moves the aliens. It works by choosing an 
alien at random, and blanking it out by DRAWing the 
character in black on the screen. 

The alien remains invisible until the alien's next position has 
been calculated by the computer. Once the new position has 
been calculated, the alien is DRAWn on the screen in 
yellow. All this happens very quickly and gives the 
impression of smooth movement, and works a little like a 
cartoon film. 


(C)F^ 

'AlrV 


tH? 


Pl' 


0 


1000 
1010 
THEN 
10.20 
1030 
1040 
105C) 
1060 
107C) 
1080 
1090 
1100 
1 1 10 


REM ALIEN 
R = INT ( RND 
RETURN 

GD’4 (X (R) ,Y(R) ,) 
HCOL.OR== 0; DRAW 3 
X(R) = X<R) + INI 
Y (R) = Y (R) + 1 


(1) *■ INVADERS) -I- 1: IF Y(R) 

== GD% (X (R) hYCR) ) 2 

AT a * X(R),8 Y(R) 

( RND (1) * 3) - 1 


IF X(R) 
IF X(R) 
IF Y(R) 
HCOLOR= 
GD"/. ( X (R) 
RETURN 


> 33 THEN X(R) = 33 
< 1 THEN X(R) = 1 
= 19 THEN LOST ~ 1 
5: DRAW 3 AT 8 X(R) 


,8 * Y(R) 


, Y (R) ) = GD"/. ( X ' R) , Y (R) ) + 


Line 1010 selects a random whole number between one and 
the value of INVADERS. The value of R identifies a particular 
alien. Line 1010 also checks the alien's vertical coordinate. If 
it is 22, then it is off-screen, and is “dead” - the subroutine 
ends with a RETURN - see the HIT subroutine later on. If a 
"living" alien has been chosen, line 1030 DRAWs an alien in 
black at Y(R), X(R) making it disappear. New coordinates 
are calculated in lines 1040 and 1050, using the RND function. 
A random number - either minus one, zero or one - is added 
to the current value of X(R) to alter the column position, and 
the value of Y(R) is increased by one. The effect of this is to 
move the alien's new position one space left and down, or 
one space down, or one space right and down. Lines 1060 
and 1070 limit the value of X(R), and make sure that the alien 
can never be off the screen. The next line, 1080, tests the 
value of Y(R). If it is equal to 19, then the alien has reached 
the bottom of the screen, and the player has lost. The LOST 
flag is therefore set. In line 1090 the alien is DRAWn in 
yellow at its new position. Finally, GD% is updated by 
adding 2 to the value in the element corresponding to the 
alien's X and Y coordinates. 


iNr(fiNP(0*3)' 

not Tot^ove 

TO ^ M&Hr 
lUT(liNP0)*3)-l 

THB RUEN MOVES C>0\NR\ 
BECRVSE / /5 RDPEO TO 
ns Y(RJ vRi-VE’.' 


24 




























■SOOI^N\ 
pet) TO 



12(3 THEN 2070 



2000 FilEM F-'L.AYEF? 

201.0 :i; = PEEK ( 1.6384); IF 1 

2020 1-4; - CHR-4i: ( ]; - 1.28) 

2030 POKE ■" 16368,0 

2040 HCOLOR^ 0; DRAW 2 AT 8 XP,19 «• 8 

2050 IF (IT == "Z" OR IT = "s") AND XP > 1 THEN XP ^ 

X F' 1. 

2060 IF (IT == "X" OR IT = ">:") AND XP < 33 THEN XP 
XP ■+■ 1 

2070 HCOLOR" :2; DRAW 2 AT 8 * XP,1V * 8 
2080 ‘ IF I == 160 THEN GOSUB 3000 
2090 RETURN 


The first of the next two subroutines allows you to control the 
player's spacecraft, and to fire missiles. The second 
subroutine animates missiles and checks for hits. 


In line 2010, PEEKing (-16384) looks at the keyboard. The 
key press is stored in 1$. The Z key will move the player’s 
spacecraft left and the X key moves it to the right. 


Line 2040 DRAWs the spacecraft in black. Lines 2050 and 
2060 control the movement of the spacecraft. IF the Z key has 
been pressed AND the spacecraft's position is greater than 
one, THEN the value of XP is decreased by one character 
position. Using the < (greater than) and the > (less than) 
operators in this way ensures that the spacecraft can never 
be offscreen. The expression XP=XP-1 is common in 
computer programming - all it means is that the variable is 
altered by the amount specified. In this case, XP is 
decreased by one. The spacecraft is held in shape number 
two and is reDRAWn in purple by line 2070 - this happens 
even if the spacecraft hasn’t moved. Line 2080 checks if the 
player has pressed the space bar (in which case 1= 160), and 
calls the fire routine if appropriate. Finally, line 2090 
RETURNS control to the main program. 




SP/X£IS 

r-x 


A 


OF p/xees 




THE Z KEY MOVES THE 
SPACESHIPCA) TO THE 
LEFT AS FAR AS COLUMN ± 


THE X KEY MOVES THE 
SPACESHIP CA) TO THE 
RIGHT AS FAR AS COLUMN 17. 



■ - 


25 




Jitaii 











































300C> 

REM FI Rt: 

301 0 

XM >;p 8 

3020 

FOR YM = :F7 TO ' 

303C) 

HCOLOR= 2: DRAW 

3040 

IF (3DX(XP,YM) > 

3C>!50 

HCOLOR- 0: DRAW 

3060 

IF GDa(XP,YM) 


0 STEF' “ 1 
1 AT XM,YM 8 
1 THEN C30SUB 4000 
:!. A T XM,YM * 8 
:1. THEN HCOLOR^^-- 7 s DRAW 


YM 

30'70 
3080 


NEXT 

RETURN 


This routine animates the missile. There is a FOR... NEXT 
loop between lines 3020 and 3070, which is set up with 
STEP-1. This is a little different from a normal FOR... NEXT 
loop because, instead of counting up, the loop counts down 
in steps of one. Whene'ver you use a FOR... NEXT loop you 
can specify whichever STEP suits your program best. In this 
case, starting at YM= 17, the missile is moved up the screen 
in steps of one line until it reaches the top line (YM=0). The 

missile is DRAWn in purple by line 3030 — HCOLOR—2 

means DRAW the shape in purple. In the next line there is a 
check to see if that position is already occupied by an alien - 
is there a hit? If the value held in GD% for that location is 
greater than one, the HIT routine starting at line 4000 is 
called. The missile is blanked out in line 3050 by DRAWing it 
in black at the same screen position. GD% is checked again 
in line 3060, but this time the check is for a star. If the missile 
has gone over a star, the star will not reappear unless it is 
DRAWn back on screen. Line 3060 takes the star’s 
coordinates and DRAWS it in again in white. 


XN,0 


AT 


THE MISSILE I MOVES 
UPWARDS AS SHOWN 
ON THE LEFT. 

O THE ! IS PRINTED. 
@ THE REVERSE g 
IS PRINTED SO THAT 
IT DISAPPEARS. 

<D THE ! IS PRINTED 
AT THE NEXT 
POSITION ABOVE. 


THE MISSILE WILL 
CARRY ON OP THE 
SCREEN UNTIL IT 
HAS HIT AN ALIEN 
OR HAS REACHED 
THE TOP OF THE 

screen. 

YOU WILL NOT BE 
ABLE TO MOVE THE 
SPACESHIP OR FIRE 
ANOTHER MISSILE 
UNTIL THE MISSILE 
HAS PISAPPEAREP 


R 


R 


R 





































































'/I 


4000 

4010 

4020 

4030 

4040 

4050 

4060 

4070 

4080 

4C>90 

4 :l. 00 

4 :l. 10 

4 :l 20 

4 :i. ::>o 

4140 


REM 

FOR A = 
IF X';A) 
HCOL.OR= 
HCOLOR- 
HC01....0R== 
GDX ■; X (A) 


Ftrr 
1 TO 


0: DRAW 
Os DRAW 
7: DRAW 
Y(A)) = 


IMOADERS 
XP OR Y<A) < 
3 AT 8 * 

1 A"r 8 * 
5 AT 8 * 
(BD/aX <A) , 



X (A) 
X (A) 




s A = INvADlERS 


A 


Y(A) 

NEXT 

ALIEN = ALIEN 1 s SC ■” SC + 

‘ IF ALIEN = 0 THEN WIN = 1 
VTAB 22s HTAB 27s PRINT SC 
HCOLOR- Os DRAW 5 AT 8 *■ Xf 
YM = O 
RETURN 




YM THEN GOTO 

4080 ■■ 1 

„ 8 * Y < A) 

,8 Y(A) 

® 1 
1 

, 8 *■ Y (A) 

) - 2 

® 1 

AHIT 0 

® 1 


© I 

i * YM 

® 1 


S 



The FOR... NEXT loop between lines 4010 and 4080 checks 
each of the invaders in turn to see which one has been hit. 

The alien which has been hit is DRAWn in black in line 4030, 
and the missile is DRAWn in black in line 4040. In line 4050 
an explosion is DRAWn in the position where the alien has 
been hit. Having destroyed the alien, line 4060 updates 
GD%. The alien's position is set to 22 so that it will be 
skipped over in line 1010. The loop is exited by setting A 
equal to INVADERS. One is subtracted from the number of 
ALIENS in line 4090, and the score (SC) increased to give you 
a running score display. The AHIT flag is reset to zero. If line 
4100 finds that no more ALIENS remain, the WIN flag is set. 
The score is displayed by line 4110, and finally the explosion 
is DRAWn over in black in line 4120. 










TH£ MISSILE MOVES UP 

W£ SCPEEN PNP /F/T 
oeracTs w rueN pbovf 

IT,IT P£&/ST£PS P HIT. 
lyE MISSILE RND THE 
PUEN PP£ THEN EPflSEJ> 
byPE[>RPIM/H 6 THE 
CHPRRCTEPS /N BLRCfC. 

UN EXPlOSm/5 OPPLVN 

IN RNP THE PUEN THEN 
HRS ITS VEPPCPL 
CXTOPPlNETES MOVEDOFF 
SCPEEAl' TV PBROVELT 
FROM PLPY. 

THE EXPlOSfOY/ /S THEN 

R0ROVEO BY REVPRIN/N& 

IN BlFtCK. 






































































The ALIEN subroutine given below has a low number 
because it is the most frequently used of the subroutines. 

The subroutine moves the aliens - represented by a pi sign. 

It works by choosing an alien at random, and then blanking it 
out by printing the character in black on the screen. The 
alien remains invisible until the alien’s next position has been 
calculated by the computer. Once the new position has been 
calculated, the alien is printed back on the screen in yellow. 
All this happens very quickly and gives the impression of 
the figure moving rapidly across the screen, and works a 
little like a cartoon film. 



.1000 
:l 010 
1 020 
1030 
;l 040 

1050 
:l 060 
1070 
1080 
1090 
1100 
1110 
1 120 


REM ALIEN 

R= I NT (RND (1) K-1NVADERS) +1 s IF Y < R) =22 1 HEN RE I URI'J 
Y=Y < R) : X = X (R) : (30S(.JB 9000 5 PR INTSPC ( X > " " ; 

GRI m. ( X , Y) =GRID-'l ( X , Y) -"2 

IF GR I D>: ( X < R) , Y (R) ) = 1 THEN GOSUB 9000; 

PRINTSPC < X)" m . " ; 

X (R) = X (R) "I" I NT (RND < 1) *3) 1 

Y < R) =Y (R) •+■ 1 

IF X(R)>36 THEN X(R)=36 
IF X (R)<1 THEN X(R)=1 
IF Y<R)=21 THEN L0ST=1 
Y“Y(R): X = X(R): G0SUB 90001 



PR I NTSPC (X ) " 


GR ID7, ( X , 
RETURN 


=GRIDX(X,Y)+2 



Line 1010 selects a random whole number between one and 
the value of INVADERS. The value of R identifies a particular 
alien. Line 1010 checks the vertical coordinate of the alien. If 
it is 22, then it is off-screen, and is "dead” - the subroutine 
ends with a RETURN instruction. What is happening here 
will become more clear when the HIT subroutine is 
described later on. If a "living alien has been chosen, line 
1020 prints a space in black at Y(R), X(R). New coordinates 
are calculated in line 1050, using the RND function. A random 
number - either minus one, zero or one - is added to the 
current value of X(R) to alter the column position, and the 
value of Y(R) is increased by one. The effect of this is to 
move the alien’s new position one space left and down, or 
one space down, or one space right and down. Lines 1070 
and 1080 set limits to the value of X(R), and make sure that 
the column position can never be off the screen. The next 
line, 1090, tests the value of Y(R). If it is equal to 21, then the 
alien has reached the bottom of the screen, and the player 
has lost. The LOST flag is therefore set. In line 1100 the alien 
is printed in yellow at its new position. Finally, GRID /o is 
updated by adding 2 to the alien’s X and Y coordinates. 


TO MOV£, tSfY* 

/NT f3 ^ 1 

HOT TO Move j 

INT(f^NP(il*3)-l=0 

TO Move right: 

INT(KN5<li3)~l=*l 
TUB ALIEN MOVES 

down siNce 1 {^,- 

f^POE^ TO THE y(fl^ 

COORPIN ATE 






















































2000 
2010 
2020 
2030 
2040 
2050 
2060 


REM PL.AYER 
GET L$ 

IF I....T=" " THEM GOSUB 3000: REM FIRE 

IF LT“"Z" AND XP>1 THEN XP=^=XP-;i. 

IF LT="X" AND XP<3'7 THEN XP^=XP+I 
Y=22: GOSUB 9000; PR IN T SPC ( XP-1) ” ?! n'ii 
RETURN 


The first of the next two subroutines allows you to control the 
player's ship, and to fire missiles. The player’s ship can move 
left and right across the bottom of the screen. The second 
subroutine animates missiles and checks for hits. 

In real-time games such as this one the player needs to be 
able to interact with what is happening on the screen. It is 
quite easy to write a routine which allows the player to use 
the keyboard to control graphics. 

In line 2010 GET looks at the keyboard to see if a key has 
been pressed, and stores the result in 1j$. Now you should 
test for the keys you are interested in. The next line tests Ij$, 
and IF it is a space (the player has pressed the space bar), 
THEN the instruction is to GOSUB 3000, which is the missile 
firing subroutine - defined below. Lines 2030 and 2040 
control the movement of the spacecraft. IF L$ is Z (the Z key 
has been pressed) AND the spacecraft's position is greater 
than one, THEN the value of XP is decreased by one. Using 
the > (greater than) and the < (less than) operators in this 
way ensures that the spacecraft can never be offscreen. The 
expression XP=XP-1 is common in computer programming 
- all it means is that the variable is altered by the amount 
specified. In this case, XP is decreased by one. The player’s 
spaceship is represented by the letter A, which is PRINTed 
on screen in a string of characters (“ a'”), between two blank 
spaces. The spaces over-print and hence erase the old 
character A when the spaceship is moved left or right. 




THE Z KEY MOVES THE 
SPACESHIP (A) TO THE 
LEFT AS FAR AS 
COLUMN 1. 


THE X XEV MOVES 
THE SPACESHIP CA) 
TO THE RIGHT AS FAR 
AS COLUMN 37 




29 





























































































1 STEP -i 


POKE V+16,,0 

POKE 'v'+lS, :i. 


REM FIRE 
>;== (XP-K-S) +24 
FOR YM~2;l. TO 
Y= < YM+8)+50 

IF X<256 THEN POKE V,Xs 
IF X >255 T" H E N P O K. E V , X -: 

POKE 'v'+1,Y2 POKEI V+21,1 
IF OR I D'X ( XP , YM) > 1 THEN GOSIJB TOO':..) 

NEXT- 

POKE V+21,0 

. . , RETURN _ 

■ ■ ■ ■ ■ ' I 

The subroutine above is the FIRE routine, which animates 
the missile. There is a FOR... NEXT loop with STEP -1 
between lines 3020 and 3080, which moves the missile from 
the bottom of the screen to the top. The sprite is displayed 
on screen using the X and Y location registers and registers 
in the VIC (video) chip. In the program these registers are 
referred to as V, V+1, V+16 and V+21. Unfortunately, you 
cannot POKE the screen coordinates directly into these 
registers The value for the X location register is calculated 
in line 3010. If X is less than 256 it is POKEd directly into V, 
along with zero into V +16, in line 3040. If X is greater than 
255 (the largest value a single memory location can contain) 
line 3050 subtracts 255 from X. Line 3030 calculates a new 
value for the Y location register. Line 3060 POKEs the Y 
location register, and switches on the sprite using the sprite 
enable register (V+21). A hit on an alien by the missile is 
detected in line 3070 and the program is directed to the HIT 
subroutine by GOSUB 4000. 


© 

1 

t 

WHEN A MISSILE C!) 
HITS A CHARACTER 
WITH A STAR, THE 
STAR IS BLOTTED 

outCO. after the 

MISSILE HAS been 

t 


■ 

_ 

PELETEP W THE 
STAR IS REPRINTETA 
IN THE SAME 
POSITION (3). 


THE MISSII-E WILL 
CARHV ON UP THE 
SCREEN UNTIL IT 
HAS HIT AN ALIEN 
OR REACHES THE 
TOP OF THE SCREEN. 
YOU WILL NOT BE 
ABLE TO MOVE THE 
SPACESHIP OR FIRE 
ANOTHER MISSILE 
UNTIL THE MISSILE 
HAS PtSAPPEAREP. 




30 
















































































I 


4000 REM HI T' 

4010 X=X-S 

4020 IF X<256 THEN F^DKE V+2,X!; POKE V+16,0 
4030 IF X>255 THEN POKE y42,X“255; POKEV+16,: 
4040 POKE yH-3,Ys POKE y+21,2 
4050 FOR A=1 TO INVADERS 

4060 IF X(A)<>XP OR Y(A)<>YM THEN GOTO 4080 
4070 Y (A) =--22 s A= INVADERS 
4080 NEXT A 

4090 AL.IENS=AL.IENS-1 s SO^^SC+l: AHI T^^^-O 

4100 .IF AL.IENS=b THEN WIN==1 

4110 Y=YM: GOSUB 9000; PRINTSPC(XP)" " 

4120 GRIDX ( XP, YM) ==GR IDX (XP , YM> -2 
4130 PR I |■■■JT " H" : PR I NTSPC < 27) " !..g" ; SC 
4140 YM=1 
4150 RETURN 


The routine above displays the explosion sprite on screen 
after a direct hit has been scored on an alien. The X and Y 
location registers and the two registers on the VIC chip are 
used to as they were in the last subroutine. In line 4030, 
though, sprite number two is enabled by POKEing V+16 
with 2. 

After a hit, line 4070 adjusts the alien's position so that it is off 
screen - by altering its Y coordinate to 22. Line 4090 alters 
the score, and the number of ALIENS remaining, and then 
resets the AHIT flag. The alien is blanked out by calling the 
subroutine at line 9000 which positions the cursor over the 
alien and control RETURNS to the mains program. 












<S) 


(D 

It ( 


>- 


% 


k"' 

i ir 

i Jv 

i X 

'9 

il 

I 

* 

i 

1 

•• :: A 

•* ...• • 

i 






Sv! 


THE WSSILE MOVES 
UP flNb IF IT DSTECTS 
M ruen/t registers 

R HIT. THE RLIEAIS HEIR 
CO-ORO/NRTES RRE 
REMOVED OFF THE 
SCREEN. THE CURSOR 
IS POSITIONED TO 

overprint theRUEN 
RNP THE EXPWStON 
SPRITE IS TURNED ON. 
WHEN THE FROERRM 
RETURNS FRt»i THE 
HIT SUBROUTINE, 

RLL THE SPRITES 
TURNED OFF, 


I 


A 























































fps 




^ ^ ^ 




/Si /Sk 





Testing your program 

You can test the moving and firing sections of the program at 
this point before going on to the final chapter of the book. 
Follow the instructions given in the boxes below. You should 
be able to move your spaceship and attack the oncoming 
invaders. As before, if you receive an error message, check 
back through your work with the listings given. 



ifiPPLEUe 


AllEHS DESTROYED^ O 


HTi . 


Hi ri • irt 


/. ryp£ RUN ANP HIT 
I ketu^h\ 

IF everything is correct, 

YOU 5HOOLP BB ABLE TO 
SHOOT THE ALIENS AS THEY 
ADVANCE DOWN THE SCREEN. 
THE PROGRAM WILL STOP 
IF you KILL THEM ALL, OR 
ONE OF THEM REACHES 
THE BOTTOM OF THE SCREEN. 
DELETE THE DUMMY LINE 
BEFORE PROCEEDING. 

ignore: the error MESSRGC 

PRINTED AT THIS STAee. 


COh/IMOPORB 

I type "RUN f\NP H/r 

\RBri/P.N\ 

IF everything is correct, 

you SHOULD BE TO 

shoot the aliens as they 

ADVANCE DOWN THE 

the program will STOP 
IF ONE OF THEM 
THE BOTTOM OF THE SCREEN^ 

delete the dummy line 

BEFORE PROCEEDING. 
leNOfRE THE ERROR MaS5AG£\ 
PRINTED AT THIS STAGE. 






























































































WINNING 
AND LOSING 

The winning and losing section is all that remains to make the 
game complete. If the player succeeds in shooting all ten 
aliens, another wave of attack appears, beginning lower 
down the screen and giving the player less time to deal with 
the attack. At the end of this section there are some tips to 
make the program even more effective, and the program 
listing is given in full. 


T H E 

I N U «=» l> E 
H fi U E 

I N l> E 


HOWEUER. 
ilJ i:>ESTROVEl> 
<!^E I EN CRU-iET 


S' o u 


E I OilT <-lOEi :i; N 


33 



































If the player is skilful enough to destroy all the aliens in a 
wave, then the WIN flag is set in the HIT subroutine, and the 
control program calls the WIN subroutine. If the player lets 
the aliens through, then the LOSE subroutine is called 
instead. 


5C)00 

REM 


W1IM 

5010 

TEXT 

: HOME 

5020 

VTAB 

12 

2 HTAB 12: PRINT "WELL DONE 

5030 

HTA.B 

1 

. pRiiMT -this TIME YOU WIN" 

5040 

HTAB 

14 

: PI-;INT "NOW PREPARE FOR " 

5050 

HTAB 

14 

: PRINT "OUR NEXT ATTACK" 

5060 

FOR 

D 

1 TO 1500: NEXT D 

5070 

Al... I EM 


INVADERS: HEIGHT == HEIGHT + 

5080 

FOR 

A 

1 TO INVADERS 

5090 

X (A) 

= A 

* 3:Y(A) =■ HEIGHT 

5100 

NEXT 

A 


5 :l 10 

RETI..)RN 



EARTHLING" 


;WIN 0 




Line 5010 clears the screen and sets the cursor to the HOME 
position (the top left hand corner of the screen) and 
switches to TEXT mode. Lines 5020, 5030 and 5040 PRINT a 
“Well Done” message in the top half of the screen, telling the 
player that another wave of aliens is on its way. Notice that 
each message line is preceded by HTAB so that the 
complete message is arranged in the correct place in the 
center of the screen. A FOR... NEXT loop at line 5060 
introduces a time delay so the player can read the message. 

This kind of delay asks the computer to count from one to 1500 
- while It is counting it cannot get on with the next part of the 
program. If the message was shorter, the computer could be 
asked to count to a smaller number (a shorter delay), or if the 
message was longer, the computer could be asked to count 
to a larger number (a longer delay). 

The number of ALIENS is reset to the number of INVADERS 
in line 5070, and the WIN flag is switched "off - i.e. becomes 
zero - ready for the next wave of aliens. The value of 
HEIGHT IS increased by 2, making the next wave of aliens 
appear lower down the screen, and increasing the level of 
difficulty. The FOR... NEXT loop between line 5080 and line 
5100 calculates the X and Y positions of each alien, by 
working out the X position from the alien number (A), and 
taking the Y position from the HEIGHT value. This is just the 
same as was done earlier in the INITIALIZE subroutine. This 
latest subroutine can be tested as before, by following the 
instructions in the box on the facing page. 

















































6000 

FdEM 

L.OSE 



60 :i. 0 

TE.XT 

; HOME 



6020 

VTA.B 

b s 

HTAB :l.5: PRINT "T H E" 



60i::-0 

HTAB 

12s 

PRINT "A L I E N S" 


0 

6040 

HTAB 

14 

PRINT "H A V E " 



t.) y) *3 

HTAB 

12s 

PRINT "L A N D E D” 


0 

6060 

VTAB 

12s 

HTAB IT: PRINT "HOWEVER” 


6070 

HTAB 

10 s 

P R I IM r " Y 0 1...I D E 3 T R 0 Y E D ' ' 

; SC 


6080 

HTAB 

12s 

PRINT "ALIEN CRAFT" 


W) 

6090 

VTAB 

20 s 

HTAB 4: PRINT "DO YOU 

WISH 


1 


TO F'le^HT 


m 


AGAIN'' 
6 :i. 00 
6 1 :l. 0 
6120 


FNIICE 
IF AT == 
RETURN 


16368,0" SET AT 
"Y" OR AT "y" 


I'HEI'-J RUN 




When the aliens land the LOSE subroutine (above) is called. 
The high resolution screen is replaced by the text screen for 
the message. 

The screen is cleared and a message is PRINTed on the 
TEXT screen, telling the player that the invaders have 
landed. Next, lines 6060 to 6080 tell the player his or her 
score. The semi colon (;) in line 6070 ensures that 
the score appears alongside the "YOU ARE DESTROYED” 
message. After a pause, the player is given the option of 
playing again. If the response is a Y, then the program RUNs 
again, otherwise the program RETURNS to the control 
program, and ENDs at line 90. 


RPPLEUe YOU CAN NOW RUN THE PROGRAM IN 

FULL. WHEN THE INVADERS ARE ALL SHOT DOWN, THE 
MESSAGE SHOWN LEFT IS DISPLAYED, AND THE GAME 
CONTINUES WITH THE ALIENS ADVANCING FROM A 
POSITION LOWER DOWN THE SCREEN. IF AN INVADER 
REACHES THE BOTTOM OF THE SCREEN, YOU HAVE LOST 
AND THE SCREEN WILL DISPLAY THE MESSAGE ON THE RIGHT. 



WELL DONE EARTHLING 
THIS TIME YOU WIN 
NOW PREPARE FOR 
OUR NEXT ATTACK 


ALIENS DESTR0YED=10 


THE 

ALIENS 
HAVE 
L A N,D E D 

HOWEVER 

YOU DESTROYED 7 
ALIEN CRAFT 

DO YOU WANT TO FIGHT AGAIN? 



35 











































If the player is skilful enough to destroy all the aliens in a 
wave, then the WIN flag is set in the HIT subroutine, and the 
control program calls the WIN subroutine. If the player lets 
the aliens through, then the LOSE subroutine is called 
instead. 



Lines 5010, 5020 and 5030 PRINT a “Well Done” message in ' 
the top half of the screen, telling the player that another 
wave of aliens is on its way. Notice that each message line is 
preceded by SPC so that the complete message is arranged 
in the correct place in the center of the screen. A FOR.. ■ 
NEXT loop at line 5060 introduces a time delay so the player 
can read the message. This kind of delay asks the computer 
to count from one to 1500 — while it is counting it cannot get on 
with the next part of the program. If the message was 
shorter, the computer could be asked to count to a smaller 
number (a shorter delay), or if the message was longer, the 
computer could be asked to count to a larger number (a 
longer delay). 

The number of ALIENS is reset to the number of INVADERS 
in line 5070, and the WIN flag is switched “off - i.e. becomes 
zero - ready for the next wave of aliens. The value of 
HEIGHT IS increased by 2, making the next wave of aliens 
appear lower down the screen, and increasing the level of 
difficulty. 

The FOR... NEXT loop between line 5080 and line 5110 
repositions the X and Y positions of each alien, by working 
out the X position from the alien number (A), and taking the 
Y position from the HEIGHT value. This is just the same as 
was done earlier in the INITIALIZE subroutine. This latest 
subroutine can be tested as before, by following the 
instructions in the box on the facing page. 























































i> e: r s" 


REM LOSE 
PRIMT"L«" 

PRINTSPC(:l.7) "P:1T H EI" 

PRINTSPC(:l.2) " I H U A 
PR INTSPC (:l. 6) " H A U El" 

PRIMTSPC (14) " L. A M ll> E l>" 

PR I NT SPC •; 16) '' tiMiLLinH O WE U E R 
i“:'R :i: N T SPC (12) " V O E# 8> fil S T ffi!; t:> V E 8> 

PR :i: N'T'SPC (14) " A 8 ... I E; 81 AT R A 8- - "I" " 

P RIN T S P C (7) ‘' » O V 818 8.1 8^811 S 8-8 

A83 A 181" ; INPUT AT 
IF' 1....EFTT (AT , 1) = " V " T HEN RUN 
RETl..)RN 


SC 


T 83 S-'■ 183 H T 






The final section to be defined is the LOSE subroutine, 
starting at line 6000. The screen is cleared and a message is 
PRINTed, telling the player that the invaders-have landed. 
Next, lines 6060 to 6080 tell the player his or her score. The 
semi colon (;) at the end of line 6070 ensures that the score of 
the number of aliens killed (SC) appears alongside the "YOU 
DESTROYED" message. 

The player is given the option of playing the game again 
after a pause. Anything the player types is given the label 
A$. If the response is any word which starts with a Y, line 
6100 RUNS the program again, otherwise the program 
RETURNS to the control program, and stops at the END 
statement in line 90. 


CJOMMOOOne YOU CAN NOW RUN THE. PROGRAM IN FULL. 

WHEN THE INVADERS ARE ALL SHOT DOWN, THE MESSAGE 
SHOWN left is DISPLAYED, AND THE GAME CONTINUES 
WITH THE ALIENS ADVANCING FROM A POSITION LOWER 
DOWN THE SCREEN. IF AN INVADER REACHES THE 
BOTTOM OF THE SCREEN, YOU HAVE LOST AND THE 


II If IIH E F A R I H L I HG 

iHjs riMF vnii NIN 

NOM I^KFRARF HIR 
nilR NFX I A I I AEK 





1 H 

-1 


I N 

U A 

D t k S 



H A 

U E ^ 


I 

A N 

l> r D 



HOME 

UFk . 

ROS'i D ^ 


VtHI 

l>t G 1 


Al 

I FN 

EkAF 1 


DO VOU WTSH 111 ElOIII AGHlN ? 




_ 



37 
















































m 








f\ 


OO 0 C) 

Improve your program 

As It stands, the Invaders game is fun and challenging to 
play. It is difficult to shoot down all the aliens and they 
appear to move as if they had a will of their own! However, it 
becomes much more like an arcade-style game if you add 
some simple sound effects for firing the missiles and alerting 
an alien attack. Given below are some hints on how you can 
add to the program to get a really professional-looking game 
that you and your friends can enjoy. 




^ ^ 0 (3 





Adding sound 

Add these lines to your program to give sound effects - a 
movement blip, an explosion noise, and a battle siren. 

Both the Apple and the Commodore generate sound effects 
when you POKE values into special memory locations. The. 
Apple program has a special machine code routine which 
plays the sound effects, stored as DATA in line 9070. Every 
time a sound effect is needed, a value is POKEd into memory 
locations 776 and 777 to set pitch and note length before the 
machine code routine is called. The Commodore POKEs 
values into registers in a special sound synthesizer chip to 
produce its sounds. 

APPLE He _ 


1;}. 05 

FhDKE 

77ti., I36s POKE 7 

...y ..... .. ^ 

.' I| •...» --^ » 

4055 

FhDKE 

776,254s POKE 7 

77 , 1.00 

7095 

FDR I 

j. TO 20 s F"'0H; 

.E. / / , ‘ 

B135 

FOR E 

; - 776 TO 79£is 

READ B 

9070 

DATA 

255,255, 1.73,48 

;, 1.92 , :l: 

a... •( .1 

4 

5, 1 74,8,3,76,1.0 

, 3 , 96 


CALL 778s REM BLIP 
i CAL..L- 7 7'8 

?0; POKE 777.,305 CAL 




)9 



























































































































Extra Graphics 

The graphics additions to the program are used after the 
aliens have landed. The Apple program uses the FLASH 
command in line 6015 to highlight the message printed in 
lines 6020 to 6050. NORMAL in line 6055 stops the following 
text from flashing. In the Commodore program the border is 
flashed by POKEing the border color location with a range of 
color codes. 



APPLE He 


6015 


FLASH 




COMMODORE 




39 

































































The complete listing 

Now that you have the complete program keyed in, you 
should store it on tape for future use. The instructions for 
saving programs vary slightly between different machines, 
and you should consult your user’s manual for detailed 
instructions. The full listing for the Space Invaders program 
is given below, for both machine versions. The additional 
material suggested on the previous page has been included. 



APPLE He 



COMMODORE 


INITIALIZE 

SCREEN 

ALIEN 

PLAYER 

1 TMEN 


GOTO 70 


GOSUB 5000; GOTO 20 
LOSE 


REM INVADERS 
GOSUB 8000: REM 
GOSUB 7000; REM 
GOSUB 1000: REM 
GOSUB 2000; REM 
IF WIN = 1 OR LOST 
GOTO 30 

IF WIN = 1 THEN 
GOSUB 6000; REM 
END 

1000 REM ALIEN 

1010 R = INT ( RND (1) ♦ INVADERS) +1: IF Y(R> = 22 
THEN RETURN 

1020 GD7.(X (R) ,Y(R) ) = GD"/. ( X (R) , Y (R) ) - 2 
1030 HCOLOR= 0; DRAW 3 AT 8 * X(R),8 ^ 

1040 X(R) =-- X<R) + INT ( RND (1) * 3) 

1050 Y(R) = Y(R) + 1 
1060 IF X(R) > 33 THEN X(R) = 33 
1070 IF X(R) < 1 THEN X(R) = 1 
1080 IF Y(R) = 19 THEN LOST = 1 
1090 HCOLOR= 5; DRAW 3 AT 8 * X(R),8 ■ 

1100 GD'/. (X <R) ,Y(R) > = GD7.(X (R) ,Y(R) ) + 2 
1105 POKE 776,136; POKE 777,30: CALL 778; REM 


Y(R) 
-- 1 


Y(R) 


11 10 

RETURN 


2000 

REM PLAYER 


2010 

I = PEEK ( - 163 

84); IF 

2020 

I^ = CHRT (I - 1 

28) 

2030 

POKE - 16368,0 


2040 

HCOLOR= 0; DRAW 

2 AT 8 + 

2050 

P - 1 

IF (IT = "Z" OR 

IT = "z' 

2060 

IF (IT = "X" OR 

IT = ">{’ 

XP + 

1 


2070 

HCOLOR= 2; DRAW 

2 AT 8 H 

2080 

IF I = 160 THEN 

GOSUB : 

2090 

RETURN 


3000 

REM FIRE 


3010 

XM = XP ♦ 8 


3020 

FOR YM = 17 TO 0 STEP - 

3030 

HCOLOR= 2; DRAW 

1 AT XM 

3040 

IF GD7. (XP,YM) > 

1 THEN 

3050 

HCOLOR= 0: DRAW 

1 AT XM 

3060 

IF GD7. (XP,YM) = 

1 THEN 

YM * 

8 


3070 

NEXT 


3080 

RETURN 


4000 

REM HIT 


4010 

FOR A = 1 TO INVADERS 

4020 

IF X(A) < > XP 

OR Y(A) 

4030 

HCOLOR= 0: DRAW 

3 AT a 

4040 

HCOLOR= 0: DRAW 

1 AT 8 

4050 

HCOLOR= 7: DRAW 

5 AT 8 

4055 

POKE 776,254; POKE 777, 


128 THEN 2070 


* XP,19 * 
) AND XP 


< 33 THEN XP 


GOSUB 4000 


HCOLOR= 7; DRAW 4 AT XM, 


< .> YM THEN GOTO 4080 

X(A),8 * Y<A) 

X (A) ,8 * Y (A) 

X(A),8 * Y(A) 


4060 GD7.(X(A) ,Y(A) ) = GD7. ( X (A) , Y (A) ) - 2 

4070 Y<A) = 22:A = INVADERS 
4080 NEXT A 

4090 ALIEN “ ALIEN - 1:SC = SC + 1;AHIT - O 

4100 IF ALIEN = O THEN WIN = 1 

4110 VTAB 22; HTAB 27; PRINT SC 

4120 HCOLOR= 0; DRAW 5 AT 8 * XP,8 * YM 

4130 YM = 0 

4140 RETURN 

5000 REM WIN 

5010 TEXT ; HOME 

VTAB 12: HTAB 12: PRINT "WELL DONE EARTHLING" 
HTAB 13; PRINT "THIS TIME YOU WIN" 

HTAB 14: PRINT "NOW PREPARE FOR " 

HTAB 14; PRINT "OUR NEXT ATTACK" 

FOR D = 1 TO 1500: NEXT D 
5070 ALIEN = INVADERS: HEIGHT = HEIGHT +■ 2; WIN = O 
5080 FOR A = 1 TO INVADERS 
5090 X(A) = A * 3;Y(A) = HEIGHT 
5100 NEXT A 
5110 RETURN 


5020 

5030 

5040 

5050 

5060 


5 REM INVADERS 

10 GOSUB 8000: REM INITIALIZE 

20 GOSUB 7000: REM SCREEN 

30 GOSUB 1000: REM ALIEN 

40 GOSUB 2000: REM PLAYER 

50 IF WIN=-1 OR L0ST=--1 THEN GOTO 70 

60 GOTO 30 

70 IF WIN=1 THEN GOSUB 5000; GOTO 20 
80 GOSUB 6000; REM LOSE 
90 END 

1000 REM ALIEN 

1010 R=INT(RND(1)*INVADERS)+1; IF Y<R)=22 THEN RETURN 
1020 Y=Y(R); X=X(R): GOSUB 9000; PRINTSPC(X)" "; 

1030 GRI D“/: ( X , Y) =GR ID7. ( X , Y) --2 

1040 IF GRID7.(X (R) ,Y(R) )=1 THEN GOSUB 9000; 

PRINTSPC<X) "IJ."; 

1050 X(R)=X(R)+INT(RND(1)*3)-1 
1060 Y(R>=Y(R)+1 
1070 IF X(R>>36 THEN X(R)=36 
1080 IF X(R)<1 THEN X(R>=1 
1090 IF Y(R>=21 THEN L0ST=1 

1100 Y=Y (R) ; X = X ( R > ; GOSUB 9000: , PR INTSPC ( X > " {Tiinr" ; 

1105 GOSUB 9100: REM BLIP 

1 IT 0 GR ID7. ( X , Y) =GR ID7. ( X , Y) +2 

1120 RETURN 

2000 REM PLAYER 

2010 GET Lt 

2020 IF Li:=" " THEN GOSUB 3000; REM FIRE 

2030 IF Lt="Z" AND XP>1 THEN XP=XP-1 
2040 IF L^="X" AND XP<37 THEN XP=XP+1 
2050 Y=22: GOSUB 9000: PRINTSPC (XP-1) " »=* 

2060 RETURN 

3000 REM FIRE 

3010 X=(XP*8)+24 

3020 FOR YM=21 TO 1 STEP ~1 

3030 Y=<YM*8)-*-50 

3040 IF X<25.6 THEN POKE V,X; POKE V+16,0 

3050 IF X>255 THEN POKE V,X-255: POKE V+16,1 

3060 POKE V-t-l,Y; POKE V+21,1 

3070 IF GR ID7. ( XP, YM) > 1 THEN GOSUB 4000 

3080 NEXT 

3090 POKE V+21,0 

3100 RETURN 

4000 REM HIT 

4010 X=X-8 

4020 IF X<256 THEN POKE V+2,X: POKE V+16,0 
4030 IF X>255 THEN POKE V+2,X-255: POKEV+16,2 
4040 POKE V+3,Y; POKE V+21,2 
4045 GOSUB 9200; REM EXPLOSION NOISE 
4050 FOR A==l TO INVADERS 

4060 IF X(A)<>XP OR Y(A)<>YM THEN GOTO 4080 
4070 Y(A)==22; A= INVADERS 
4080 NEXT A 

4090 ALIENS=AL1ENS-1; SC=SC+1; AH11=0 

4100 IF ALIENS=0’THEN WIN=1 

4110 Y=YM; GOSUB 9000; PRINTSPC(XP)" " 

4120 GR ID7. ( XP , YM) =GR ID7. ( XP , YM) -2 

4130 PR I NT " ltd" : PR I NTSPC (27) " \M ''; SC 

4140 YM=1 

4150 RETURN 

5000 REM WIN 

5010 PRINT"tJ" 

5020 PR I NTSPC (10) " L L. DONE EARTHLING' 

5030 PR I NTSPC ( 1 1 ) " L'JT H l S TIME V OIJ WI N " 

5040 PR I NTSPC (12)" LlL'IT^aN O W PREPARE FOR " 

5050 PR I NTSPC (12)" L'30 UR NEXT A T TACK" 

5060 FOR D=1 TO 1500; NEXT D 

5070 LET ALIENS=INVADERS; HEIGHT=HEIGHT+2; WIN=0 

5080 FOR A=1 TO INVADERS 

5090 LET X(A)=A*3; Y(A)= HEIGHT 

5100 GRID7.(X(A),Y(A))=2 

5110 NEXT A 

5120 RETURN 


40 









































I III F 1 C H I 


COMMODORE continued 

6000 REM lose 
6010 PRimfL-" 

6020 PRINTSPC(J 7)" I 11 t " 

6030 PRINrSPC( 1 " 1. N U r-n D F S" 

6040 PRINTSPC< 16) " H rt U F '' 

6050 PRINTSPC<14) " I Ft H l> E D" 

6060 PR 1 NT SPC (16)" K 3«-« O HE U E: R , " 

6070 PRINT3PC< 12) •'veil 1>ES FRUVE I> " ; SC 
6080 PR I NTSPC ( 1 4) " A L I I: H C R A F I " 

6085 FOR C=1 TO 15 

6086 P0KE53280,C 

6087 FOR D=1 TO 300:NEXT 

6089 NEXT 

6090 PR 1 NTSPC (7) " 14.VL‘E'i! JD I) V O U WI 
AO A I N" INPUT AT 

6100 IF LEFTT(AT,1)=" V " T HEN RUN 

6110 RETURN 

7000 REM SCREEN 

7010 POKE 0+21,0: PRINT" U" 

7020 FOR S=1 TO 40 

7030 X= I NT (RND (1) *40) : Y= I NT (RND (2) *2.1 ) : PR I ITT " : 

G0SUB9000: PR INTTAB(X) " {M . " 

7040 f3RID‘4<X , Y)=l 
7050 NEXT S 

7060 PR 1 NT ■' tfj" : RRI NTSPC ( 10) " A1... 1. E NS l> E S I R O V E l> " ; SC 

7065 GOSUB 9300: REM BATTLE SIREN 

7070 RETURN 

8000 REM INITIALIZE 

8010 POKE 53280,0: POKE 53281,0: POKE 650,255: 

PRINT"L-" 

8020 PR I NTSPC (6) " fc, V OII A R E A ION E S A C E P I I t) T " 
8030 PR IN rSPC (4) " F» R O I E C 1 1 N G IMF' PI A N E I 

FART FI. IN' 

8040 PRINTSPC(4) " A EEW MOMENTS VOU Will BE 

oni>e:r" 

8050 PR I NTSPC ( 4 > " A I I A C K B V A I 1 E N S FROM I H E 
P L A N F T " 

8060 PR I NTSPC (4) " U A R G O H " 

8070 PR I NTSPC (4) " V O U R MISSION IS 1 O P R F U F N I 
ANV" 

8080 PR I NTSPC ( 4 ) " U A R G O N I A N S H IF* FROM 
LAND ING ." 

8090 PR I NTSPC (6) " LLL'jr^ ' Z ^ MO U E S S T A R F I G H T E R 

F EF T " 

8100 PR I NTSPC (6) " 'X ^ MO U F: S S 1 A R F I G H 1 F: R R 1 G FI T " 
8110 PRINTSPC(7) "UlIPRESS SPACE lO FIRE LASER" 

8120 PR I NTSPC (5) " L'i'* SP R E S S AN V K E V T O F H G A G E 
F NEIMV 

8130 L0ST=0: WIN=0; SC=0; AHIT=0: HITSTAR=0; XP=?0: 

HEIGHT=1: INVADERB=1O 
8140 ALIENS=INVADERS 

8150 DIM X(INVADERS): DIM Y(INVADERS): 

DIM 6RID>:(40,21 ) 

8160 FOR A=1 TO INVADERS 
81 70 X (A) = A*4--2: Y (A) =HE IGHT 
8180 GRID7. ( X (A) , V (A) ) =2 
8190 NEXT A 
8200 GOSUB 8500 
8210 

8220 GET RT; IF R;t:="" THEN 8220 
8230 RETURN 

8500 REM SPRITE DEFINITION r. 

8510 FOR N=0 TO 62: READ 0: POKE 832+N,Q: NEXT: 

REM MISSILE 

8520 FOR N=0 TO 62; READ Q: POKE 896+N,Q: NFXT: 

REM EXPLOSION 
8530 V=53248 
8540 POKE 2040,13 
3550 POKE 2041,14 
8560 POKE V+39,2 
8570 POKE V+40,7 

8580 DAT A 8,0,0,28,0,0,28,0,0,62,0,0,93,0,0,28,0,O, 

28,0,O,62,0,0,127,0,0,127,0,0 
8590 DAT A O,0,0,0,0,0,0,0,0,0,0,0,O,0,0,0,0,0,O 0 O 
0,0,0,0,0,0,0,0,0,0,0,0 
8600 DATA 0,0,0,136,0,17,2,0,64,1,0,128,16,165,8, 
129,0,129,33,0,132,18,165,72 
3610 DATA 12,90,48,18,165,72,0,92,0,0,92,0,7^ 165 IS 
48,90,12 ’ 

8620 DATA 72,165,18,132,0,33,129,0,129,8,165,16, 
128,0,1,64,0,2,17,O,136 
8630 return 

9000 PRINT"fea": FOR 1=1 TO Y: PRINT"LU";; NEXT; RETURN 
9,100 REM BLIP 

911O P0KE54276,0:P0KE54277,0:P0KE54272,0 
9120 P0KE54296,15:P0KE54277,128:P0KE54276,’% 

9130 P0KE54273,30:P0KE54272,75 
9140 RETURN 

9200 REM EXPLOSION NOISE 

9210 P0KE54283,129;P0KE54284,15:P0KE54280,4o 

9220 FOR P=15 TO 0 STEP -1:P0KE 54296,P:NEXT 

9230 P0KE54283,0 

9240 RETURN 

9300 REM BATTLE SIREN 

9310 FOR S=1 TO 20 

9320 P0KE54276,0:P0KE54277,O:P0KE54272,0 
9330 PDKE54296,15; P0KE54277,64;P0KE54276,33 
9340 P0KE54273,50:P0KE54272,37 
9350 NEXT 
9360 RETURN 


6000 REM LOSE 
6010 TEXT : HOME 
6015 FLASH 

6020 VTAB 5: HTAB 15: PRINT "T HE" 
6030 HTAB 12; PRINT "A L I E N S" 
6040 HTAB 14; PRINT "HAVE " 

6050 HTAB 12: PRINT "L A N D E D" 


605 


NORMAL 


6060 VTAB 12: HTAB 14; PRINT "HOWEVER" 

6070 HTAB 10: PRINT "YOU E^ESTROYED "; SC 
6080 HTAB 12; PRINT "ALIEN CRAFT" 

6090 VTAB 16; HTAB 4; PRIMf "DO YOU WISH TO FIGHT 
AGAIN?" 

6100 POKE 


16368,0; GET AF 
"Y" OR A4 = "y" 


THEN RUN 


6110 IF A# = 

6120 RETURN 
7000 REM SCREEN 
7010 HGR ; HOME 
7020 POKE - 16301,0: POKE 232,252: POKE 233,29; SCAL 
E= 1 

7030 FOR S = 1 TO 20 

7040 DF =INT ( RND (1) * 35):GH =INT ( RND (1) * 1 9) 

7050 GD7. (DF,GH) = 1 

7060 HCOLOR= 7: DRAW 4 AT 8 * DF,8 * GH 
7070 NEXT S 

7080 VTAB 22: HTAB 10: PRINT "ALIENS DESTROYED=";SC 

7090 HCOLOR= 2: DRAW 2AT8*XP,8*19 

7095 FOR I = 1 TO 20: POKE 776,90; POKE 777,30; CALL 

778; NEXT 

7100 RETURN 

8000 REM INITIALIZE 

8010 TEXT ; HOME 

8020 VTAB 2; HTAB 7: PRINT "YOU ARE A LONE SPACE 
PILOT" 

8030 HTAB 5: PRINT "PROTECTING THE PLANET EARTH. IN" 
8040 HTAB 5; PRINT "A FEW MOMENTS YOU WILL BE UNDER" 
8050 HTAB 5: PRINT "ATTACK BY ALIENS FROM THE PLANET" 
8060 HTAB 16; PRINT "VARGON." 

: PRINT "YOUR MISSION IS TO PREVENT ANY" 
PRINT "VARGONIAN SHIP FROM LANDING." 


"'Z' MOVES STARFIGHTER 
"'X' MOVES STARFIGHTER 


8070 HTAB 
8080 HTAB 

8090 VTAB 10: HTAB 7: PRINT 
LEFT" 

8100 VTAB 12; HTAB 7: PRINT 
RIGHT" 

8110 VlAB 14: H1AB 7: PRINT "PRESS SPACE BAR 10 FIRE 
MISSILE" 

8120 VTAB 18: HTAB 5; PRINT "PRESS ANY KEY TO ENGAGE 
ENEMY" 

8130 GOSUB 9000 

8135 FOR E = 776 TO 798; READ B; POKE E,B; NEXT 
8140 LOST = 0:WIN = 0:SC = 0:AHIT = 0;HITSTAR = 0:XP = 
16:HEIGHT = 0;INVADERS = 10:ALIEN = INVADERS 
8150 ALIENS = INVADERS 

8160 DIM X( INVADERS): DIM Y( INVADERS); DIM GD7.(35,19) 

8170 FOR A = 1 TO INVADERS 

8180 X(A) = A * 3:Y(A) = HEIGHT 

8190 NEXT A 

8200 GET R4: 

8210 RETURN 

9000 FOR X = 7676 TO 7804 
9010 READ A 
9020 POKE X,A 
9030 NEXT X 

9040 DATA 5,0,12,0,25,0,53,0,80,0,86,0,73,169,63,46, 
53,63,46,53,63,22,45,5,O0,73,45,109,58,223,27,55,109,7 
3,53,255,219,55,109,73,53,63,63,63,55,54^37,108,73,53, 
55,45,00,41,77,9,245,59,63,159,45,45,45,255,219,55,45, 
45,45 

9050 DATA 245,59,63,159,46,108,73,53,63,00,73,145,58 
,46,5,00,13,77,13,182,58,223,36,220,147,46,61,22,109,1 
13,43,109,57,4,193,36,4,193,193,32,36,223,255,27,255,5 
1,150,173,191,82,118,182,41,69,41,5,193,193,1,00 
9060 RETURN 

9070 DATA 255,255,173,48,192,136,208,5,206,9,3,240,9 
,202,208,245,174,8,3,76,10,3,96 




£ 

i 

I 



































IF 


Glossary 


Array 


DELAY 


An array is a set of data, held together and identified by one 
variable name (see also the entry for variable). One way of 
imagining an array is as a series of boxes within the 
computer's memory, with each separate piece of data held 
in a separate box. 

Delays are sometimes included in computer programs when 
it is necessary to slow the computer down. They are usually 
part of a FOR... NEXT loop (see below) and look like this in 
a program: 

FOR DE = 1 TO 1000; NEXT DE. 

This would cause the computer to count to 1000 before going 
on to the next stage of the program. 

DIM The BASIC instruction for opening an array. It is followed by 
a number in brackets which tells the computer how big the 
array should be. 

DRAW Is an APPLE BASIC instruction to DRAW a shape on the 
screen. It takes the form DRAW N AT X,Y where N is the 
shape table number and X and Y are the screen co¬ 
ordinates where the shape is to be drawn. 

flag A flag is an operator within a program that can be “set" to 
either "on" or "off, depending on certain conditions. These 
are often used in games to determine whether or not a game 
is won or lost - if the game is won, then a “win flag” can be set 
from “off, to “on" and the appropriate action is then taken. 
Flags are given the values of either 0 or 1, corresponding to 
“off and “on" respectively. 

GOSUB GOSUB XXXX sends control of the program to a subroutine 
starting at line XXXX. The search for line XXXX starts at line 
0 - so the program will run faster if subroutines that are 
called most often are placed near the start of the listing. 

GOTO This instruction tells the computer to go to the Specified line, 
missing out any lines in-between. It is used with IF... THEN 
(see below). Be careful when using GOTOs, as it’s easy to 
have the program jumping backwards and forwards so much 
that it is impossible to read. 

THEN This is used as a way of telling the computer to do something 
only when certain conditions are true. This instruction often 
looks something like this; IF score= 10 THEN PRINT “WELL 
DONE, YOU'VE WON!!!" 




























INPUT 

This instruction allows the computer to be given information 
while a program is running. When the computer comes to an 
INPUT instruction it prints a question mark (or, for some 
computers, a different symbol) to prompt the user, and waits 
for the input to be given. 

INT 

INT is short for integer, and instructs the computer to make a 
whole number of a figure with decimal places in it. It is often 
used in conjunction with the RND command which instructs 
the computer to generate a random number (see below). 

LET 

This is one way of giving the computer information. In some 
programs there may be statements such as: X= 10 

This simply means that the number ten is stored under the 
label X. It is often clearer to write: 

LET X= 10 

The LET statement also gives rise to something that at first 
sight seems illogical, if not impossible. In many programs 
you will see things like: 

LET X=X+1 

Of course, in mathematical terms X can't equal X+1. All this 
type of statement means is “increase the value of whatever is 
stored in X by one." 

LIST 

This makes the computer display whatever program is has in 
its memory. You can LIST single lines, or parts of a program 
by following the LIST command with the appropriate line 
numbers. 

PEEK 

Is an instruction to look at the number which is contained 
withm a specific memory location given in brackets. For 
example PEEK (12345) would look into memory location 12345. 

POKE 

This instructs the computer to store a piece of information at 
a particular memory location. For example, the instruction: 
POKE a,b tells the computer to place the information b in 
memory location a. 

RETURN 

This is the signal to end a subroutine. RETURN causes 
control of the program to go back to the statement following 
the most recently executed GOSUB. 

RND 

This instruction makes the computer generate a random 
number. The precise instruction varies between different 
models of computer. Both the Apple He and the Commodore 

64 generate random decimal numbers between 0 and 1. To 
make whole numbers between a desired range this is 
multiplied by a suitable figure and made a whole number 
using INT. 


43 















SPRITE In COMMODORE BASIC a sprite is a high resolution 

programmable object that can be made into any shape and 
moved about the screen using BASIC instructions. They are 
extremely useful for creating smooth animation - you can tell 
your sprite to move behind or in front of anything else on the 
screen. Even collisions between other sprites can be 
detected. 

SHAPE TABLE In APPLE BASIC a shape table is a sequence of numbers 

that is stored in the computer’s memory. These numbers can 
be used by a short routine that creates graphic figures on the 
screen by interpreting the numbers as "move only" or “plot 
and move" instructions. You can draw the shape anywhere 
on the screen using the DRAW instruction. 


Index 


A 

FOR...NEXTloop8,17,21, 26, 

R 

animation 8 

27, 34, 36 

random numbers 17, 24, 28, 42 

array 16, 20, 21, 42 


registers 9,11, 21, 30, 31, 38 


G 

REM 14, 18 

B 

graphics 9,11,14,17, 24, 25, 39 

RND 24, 42 

bit 10,11 

graphics grid 16, 21 


byte 11, 21 


S 


I 

saving program 40 

c 

IF...THEN 14,18, 29, 42 

score 27, 35, 37 

colors 19, 21, 24, 26, 39 

integer 16, 42, 43 

shape tables 10 

control program 12,13, 21, 34, 


sound 38 

35, 36, 37 

L 

SPC commands 19, 36 

coordinates 17, 24, 27, 28, 30, 

loop 16, 26, 27 

sprites 9,10,11, 21, 30,31 

31, 43 

losing 33 

subroutine 13 

cursor 15, 31, 34 


• 


M 

T 

D 

memory location 11, 21, 30, 38 

testing 22, 32 

delay 8, 34, 36 

moving 23 

text screen 15, 35 

F 

P 

U 

firing 23, 33 

pixels 10, 24, 28 

user-defined characters 10,11 

flags 14,16,18, 20, 24, 27, 28, 31, 

program, improving 38 


34, 36, 42 


V 

flowchart 12,13 


variables 8,14,16,18, 29, 43 


44 















Design 

Cooper • West 

Editor 

David Rosam 

Program editors 

Steve Rodgers 


Illustrators 

Gerard Brown 
Andrew Farmer